<template>
  <div>
    <el-row>
      <el-col :span="24">
        <dynForm
          :info-form="infoForm"
          :search-tags="searchTags"
          label-width="120px"
        >
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="doSearch"
          >
            {{ $t('search') }}</el-button>
        </dynForm>
      </el-col>
    </el-row>
    <div class="table-buttons">
      <el-button
        v-btnPermission:add
        type="primary"
        icon="el-icon-plus"
        @click="switchHandle('add')"
      >{{ $t("add") }}</el-button
      >
      <el-button
        v-btnPermission:edit
        :disabled="UpdateDisabled"
        type="primary"
        icon="el-icon-edit"
        @click="switchHandle('modify')"
      >{{ $t("modify") }}</el-button
      >
      <el-button
        v-btnPermission:delete
        :disabled="DeleteDisabled"
        type="danger"
        icon="el-icon-delete"
        @click="switchHandle('del')"
      >{{ $t("delete") }}</el-button
      >
    </div>
    <!-- 主体部分 -->
    <el-card class="box-card" shadow="never">
      <!-- 表单部分 -->
      <el-row>
        <dynTable
          ref="dynTable"
          :loading="tableLoading"
          :table-data="tableData"
          :table-columns="tableColumns"
          :handle-selection-change="handleSelectionChange"
          :row-click="rowClick"
        />
      </el-row>
      <!--分页组件-->
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :size.sync="listQuery.size"
        @pagination="getList"
      />
    </el-card>
    <toolingModel ref="toolingModel" @initList="initList"/>
  </div>
</template>

<script>
import crudJob from '@/api/Tooling/toolingModel'
import toolingModel from './module/toolingModel'
import mySetting from '@/mixins/mySetting'

export default {
  name: 'ToolingModelPage',
  components: { toolingModel },
  mixins: [mySetting],
  // 数据字典
  data() {
    return {
      infoForm: {},
      searchTags: [
        { label: this.$t('toolingModel'), prop: 'toolingModel', tag: 'DynInput' }
      ],
      tableColumns: [
        { prop: '', label: '', type: 'selection' },
        { prop: '', label: this.$t('index'), type: 'index', width: '50px', templete: (_scope) => {
          return (
            <span>{ (this.listQuery.page - 1) * this.listQuery.size + _scope.$index + 1 }</span>
          )
        } },
        {
          prop: 'toolingModel',
          label: this.$t('toolingModel')
        },
        {
          prop: 'toolingType',
          label: this.$t('toolingType')
        },
        // {
        //   prop: 'eqpSite',
        //   label: this.$t('eqpSite')
        // },
        {
          prop: 'creator',
          label: this.$t('creator')
        },
        {
          prop: 'createTime',
          label: this.$t('createTime')
        },
        {
          prop: 'editor',
          label: this.$t('editor')
        },
        {
          prop: 'editTime',
          label: this.$t('editeTime')
        }
      ],
      tableLoading: true
    }
  },
  created() {
    this.initList()
  },
  methods: {
    switchHandle(val) {
      switch (val) {
        case 'add':
          this.handleClickAdd()
          break
        case 'del':
          this.handleDel()
          break
        case 'modify':
          this.modify()
          break
        default:
          break
      }
    },
    // 新增数据
    handleClickAdd() {
      this.$refs.toolingModel.handleOpen()
    },
    // 删除数据
    handleDel() {
      this.$confirm(
        this.$t('confirmDelete') + this.multipleSelection.length + this.$t('recordCount'),
        this.$t('tip'),
        {
          confirmButtonText: this.$t('confirm'),
          cancelButtonText: this.$t('cancel'),
          type: 'warning'
        }
      ).then(() => {
        const ids = this.multipleSelection.map((ele) => {
          return ele.id
        })
        crudJob.del({ ids: ids }).then((res) => {
          this.doMerge(res)
          this.setOnePage()
          this.initList()
        })
      })
    },
    // 修改数据
    modify() {
      this.$refs.toolingModel.handleOpen(this.multipleSelection[0])
    },
    // 切换分页
    getList(val) {
      this.tableLoading = true
      this.listQuery = val
      this.initList()
    },
    // 初始化列表
    initList() {
      crudJob.getList(this.$clearNull({ ...this.infoForm, ...this.listQuery })).then((res) => {
        this.tableData = res.data
        this.total = res.total
        this.tableLoading = false
      })
    },
    // 点击查询
    doSearch() {
      this.listQuery.page = 1
      this.initList()
    },
    // 确认新增
    doSave() {
      this.listQuery.page = 1
      this.initList()
    },
    handleSelectionChange(row) {
      this.multipleSelection = row
    },
    rowClick(row) {
      this.$refs.dynTable.$refs.multipleTable.toggleRowSelection(row)
    },
    // 关闭弹窗清空数据
    closeDialog() {
      this.isDisabled = false
      this.form = {}
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/assets/styles/defaultStyle.scss";
</style>
